package com.shareyi.simpleserver.handler;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.shareyi.simpleserver.core.HttpRequest;
import com.shareyi.simpleserver.core.HttpResponse;
import com.shareyi.simpleserver.core.SimpleHttpServer;
import com.shareyi.simpleserver.servlet.ServletException;


import sun.net.www.protocol.http.HttpURLConnection;

@SuppressWarnings("restriction")
public class HttpHandler extends Thread {

	//is filter postfix
	private static boolean postfixFilter;
	private static String[] postfixs;
	private static int access;
	private Socket connection;
	private static Log log=LogFactory.getLog(HttpHandler.class);
	public static final String INIT_WINDOW_ACITON="initWindow_jWindow";
	public HttpHandler(Socket ss)
	{
		connection=ss;
	}
	
	
	@Override
	public void run()
	{
		SimpleHttpServer simpleHttpServer=SimpleHttpServer.getServerInstance();
		HttpRequest request = null;
		HttpResponse response;

		try {
			InetAddress address=connection.getInetAddress();
			String srcIp=address.getHostAddress();
			
			//如果限定为只能本地访问
			if(access<2)
			  if(!srcIp.equals("127.0.0.1")&&!srcIp.equals("localhost"))
			  {
						return ;
			  }

			request=new HttpRequest(connection);
			int requstState=request.readRequest(connection.getInputStream());
			response=new HttpResponse(request, connection.getOutputStream());
			String url=request.getUrl();
			if(url==null){
				return ;
			}
			if(url.trim().equals("/"))
				request.setNowUrl(simpleHttpServer.getIndex());
			
			log.info("accept request:"+url);
		
			//如果request读取不正常
			if(requstState!=HttpURLConnection.HTTP_OK)
			{
				if(requstState==1000&&!simpleHttpServer.isManyInstAllow())
					requstState=2000;
				
				response.setStatusCode(requstState);
				response.doResponse();
				return ;
			}
			//根据postfix做不同的控制处理
			handleProcess(connection,request, response);
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally
		{
			try {
				//connection.getInputStream().close();
				//connection.getOutputStream().close();
				connection.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			simpleHttpServer.removeSocket(connection);	
			if(request!=null)
			log.info("request:"+request.getUrl()+ " finished!");
		}
		
	}


	public static void handleProcess(Socket connection,HttpRequest request, HttpResponse response)
			throws IOException {
		String postfix=request.getPostfix();
		//检查是否是支持的类型
		if(postfixFilter&&!checkPostfixAllowed(postfix))
			{
			response.setStatusCode(HttpURLConnection.HTTP_UNSUPPORTED_TYPE);
			response.doResponse();
			return ;
			}	
		
		try {
			FilterHander.getSingleHandler().doHandle(connection, request, response);
			response.doResponse();
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}

	
	
	public static int getAccess() {
		return access;
	}


	public static void setAccess(int access) {
		HttpHandler.access = access;
	}


	public static void setPostfixs(String[] postfixs) {
		HttpHandler.postfixs = postfixs;
	}

	

	public static void setPostfixFilter(boolean postfixFilter) {
		HttpHandler.postfixFilter = postfixFilter;
	}


	public static boolean checkPostfixAllowed(String postfix)
	{
		for(int i=0;i<postfixs.length;i++)
			{
			 if(postfixs[i].equalsIgnoreCase(postfix))
				 return true;
			}
		return false;
	}
}
